package com.myapplication.interfaces;

import com.myapplication.callback.SearchByOrderCallback;
import com.myapplication.callback.SearchCallback;
import com.myapplication.http.util.HttpFail;
import com.myapplication.http.util.Util;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by zskzh on 2016/7/2.
 */
public class SearchPageConnect implements SearchInter {


    @Override
    public void searchGoods(String keyword,String keyName, String key, String currentPage, final SearchCallback searchCallback) {
        RequestParams requestParams = new RequestParams(Util.UrlSearchPageInfor);
        requestParams.addBodyParameter(keyName, keyword);
        requestParams.addBodyParameter("key", key);
        requestParams.addBodyParameter("curpage", currentPage);
        requestParams.addBodyParameter("order","2");
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                searchCallback.onSearchSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                searchCallback.onSearchFail(HttpFail.onError(ex, isOnCallback));
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void searchGoodsByOrder(String keyword,String keyName, String key, String currentPage, String order, final SearchByOrderCallback searchByOrderCallback) {
        RequestParams requestParams = new RequestParams(Util.UrlSearchPageInfor);
        requestParams.addBodyParameter(keyName, keyword);
        requestParams.addBodyParameter("key", key);
        requestParams.addBodyParameter("order",order);
        requestParams.addBodyParameter("curpage", currentPage);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                searchByOrderCallback.onSearchByOrderSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                searchByOrderCallback.onSearchByOrderFail(HttpFail.onError(ex, isOnCallback));
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
